package com.cnaaa.pay.controller;

import com.cnaaa.pay.entity.Trade;
import com.cnaaa.pay.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 2018/10/29 Create by 郭文梁
 * TradePageController
 * 订单页面控制器
 *
 * @author 郭文梁
 * @date 2018/10/29
 */
@Controller
@RequestMapping("/trade-page")
public class TradePageController {
    private static final String CHOOSE_PAYMENT_METHOD = "choose-payment-method";
    private static final String ERROR = "error";
    private final TradeService tradeService;

    @Autowired
    public TradePageController(TradeService tradeService) {
        this.tradeService = tradeService;
    }

    @GetMapping("/pay/{id}")
    public String pay(Map<String, Object> model, @PathVariable("id") String id) {
        Trade trade = tradeService.findById(id);
        if (trade.getStatus() == Trade.CREATE && trade.getStatus() == Trade.ACTIVE) {
            //只有床渐渐状态或者活动状态的交易才允许支付
            model.put("trade", trade);
            return CHOOSE_PAYMENT_METHOD;
        } else {
            model.put("errMsg", "交易状态无效");
            return ERROR;
        }
    }
}
